package com.link2room.rodimus.facade.impl;

import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.enums.exception.RodimusSysExceptionEnum;
import com.link2room.rodimus.dto.param.manager.MemberManagerParamDto;
import com.link2room.rodimus.dto.result.base.MemberInfoDto;
import com.link2room.rodimus.entity.MemberBase;
import com.link2room.rodimus.exception.RodimusException;
import com.link2room.rodimus.facade.IMemberManagerFacadeService;
import com.link2room.rodimus.feign.soundwave.PmsMemberFeignService;
import com.link2room.rodimus.service.IMemberRankUtil;
import com.link2room.soundwave.dto.param.inside.MemberLoginInsideParamDto;
import com.link2room.soundwave.dto.result.base.PmsMemberInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by VegetaKo on 2016/11/9.
 */
@Service("memberManagerFacadeService")
public class MemberManagerFacadeServiceImpl implements IMemberManagerFacadeService {

    @Resource
    private ICommonService commonService;

    @Autowired
    private PmsMemberFeignService pmsMemberFeignService;

    @Resource
    private IMemberRankUtil memberRankUtil;

    @Override
    public void test() throws Exception {

    }

    @Override
    public QueryResult<MemberInfoDto>  getMemberInfo(MemberManagerParamDto memberManagerParamDto) throws Exception {
        MemberBase memberBase = new MemberBase();
        memberBase.setMemberName( memberManagerParamDto.getMemberName());
        memberBase.setMemberNo( memberManagerParamDto.getMemberNo());
        memberBase.setOpenid(memberManagerParamDto.getOpenid() );
        QueryResult<MemberInfoDto>  memberInfoDtoQueryResult= ClassConverUtil.copyQueryResultToDtoQueryResult(commonService.findAllEQ( MemberBase.class, memberBase),MemberInfoDto.class, false);
        return  memberInfoDtoQueryResult;
    }

    @Override
    public void unbindMemberInfo(MemberManagerParamDto memberManagerParamDto) throws Exception {
        MemberBase memberBase = new MemberBase();
        memberBase.setMemberName( memberManagerParamDto.getMemberName());
        memberBase.setMemberNo( memberManagerParamDto.getMemberNo());
        memberBase = commonService.findOneEQ( MemberBase.class, memberBase );

        //第三方账号解绑（包含LINK2ROOM，WEIXIN）
        if(memberBase!=null ){
            MemberLoginInsideParamDto memberLoginParamDto= new MemberLoginInsideParamDto();
            memberLoginParamDto.setLhotelGroupCode(memberManagerParamDto.getLhotelGroupCode()) ;
            memberLoginParamDto.setLhotelCode(memberManagerParamDto.getLhotelCode() );
            memberLoginParamDto.setL2RMemberNo( memberBase.getMemberNo());
            memberLoginParamDto.setOpenid(memberBase.getOpenid() );
            pmsMemberFeignService.unbindingPMSMember(memberLoginParamDto );
            memberBase.setIsbindPms("F");
            memberBase.setMemberLevel("L0");
            commonService.merge( memberBase);
        }
        else{
            throw new RodimusException(RodimusSysExceptionEnum.MEMBER_IS_NOE_EXIT);
        }
    }

    @Override
    public void bindingPMSMemberAll(MemberLoginInsideParamDto memberLoginParamDto) throws Exception {
        MemberBase memberBase = new MemberBase();
        memberBase.setMemberName("微信粉丝");

        QueryResult<MemberBase> memberBaseQueryResult = commonService.findAllEQ(MemberBase.class,memberBase,memberLoginParamDto.getCurrentPage() ,memberLoginParamDto.getPageSize());
        if(null!=memberBaseQueryResult &&  memberBaseQueryResult.getResultlist().size()>0 ){
            for ( MemberBase mb: memberBaseQueryResult.getResultlist()) {
                memberLoginParamDto.setL2RMemberNo(mb.getMemberNo() );
//                memberLoginParamDto.setLhotelGroupCode("QINGTENG");
                PmsMemberInfoDto pmsMemberInfoDto = null;
                try {
                    pmsMemberInfoDto= pmsMemberFeignService.checkPMSMember( memberLoginParamDto.paramToMap()).getResultInfo();
                    if(null!= pmsMemberInfoDto  ){
                        mb.setMemberName(pmsMemberInfoDto.getName() );
                        mb.setMobile( pmsMemberInfoDto.getMobile());
                        String l2rMemberLevel = memberRankUtil.groupRankTOL2RRank(pmsMemberInfoDto.getLhotelGroupCode(),pmsMemberInfoDto.getCardType(),pmsMemberInfoDto.getCardLevel());//.gcRank2L2RRank(pmsMemberInfoDto.getCardLevel());
                        memberBase.setMemberLevel(l2rMemberLevel);
                        commonService.merge( mb);
                    }
                }finally {
                    mb.setMemberName( mb.getMemberName()+"2");
                    commonService.merge( mb);
                }
            }
        }
    }
}
